﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Mrhd.Core.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Mrhd.Core.Entities
{
    /// <summary>
    /// 商品表
    /// </summary>
    [Table("Sys_Product")]
    [Comment("商品表")]
    public class ProductEntity: Entity, IEntityTypeBuilder<ProductEntity>, IEntitySeedData<ProductEntity>
    {
        /// <summary>
        /// 商品名称
        /// </summary>
        [Required]
        [Comment("商品名称")]
        [MaxLength(64)]
        public string Name { get; set; }
        /// <summary>
        /// 商品编码
        /// </summary>
        [Comment("商品编码")]
        [MaxLength(64)]
        public string Code { get; set; }
        /// <summary>
        /// 商品列表展示图片
        /// </summary>
        public string Logo { get; set; }
        /// <summary>
        /// 名称拼音
        /// </summary>
        [Comment("名称拼音")]
        [MaxLength(64)]
        public string Pinyin { get; set; }
        /// <summary>
        /// 拼音首字母
        /// </summary>
        [Comment("拼音首字母")]
        [MaxLength(64)]
        public string FirstLetter { get; set; }
        /// <summary>
        /// 规格描述
        /// </summary>
        [Comment("规格描述")]
        public string Description { get; set; }
        /// <summary>
        /// 产品单位
        /// </summary>
        [Comment("产品单位")]
        [MaxLength(64)]
        public string UnitName { get; set; }
        /// <summary>
        /// 商品单价
        /// </summary>
        [Comment("商品单价")]
        public double Price { get; set; }
        /// <summary>
        /// 商品库存
        /// </summary>
        [Comment("商品库存")]
        public int Stock { get; set; } = -1;
        /// <summary>
        /// 商品属性（JSON : [{title:"加糖", value: ["少糖", "多糖"]}]）
        /// </summary>
        [Comment("商品属性")]
        public string Attributes { get; set; }
        /// <summary>
        /// UPC码
        /// </summary>
        [Comment("UPC码")]
        [MaxLength(32)]
        public string UPC { get; set; }
        /// <summary>
        /// SKU码
        /// </summary>
        [Comment("SKU码")]
        [MaxLength(32)]
        public string SKU { get; set; }
        /// <summary>
        /// 产品状态
        /// </summary>
        [Comment("产品状态")]
        public ProductStatus Status { get; set; } = ProductStatus.Sale;
        /// <summary>
        /// 上架时间
        /// </summary>
        [Comment("上架时间")]
        public DateTimeOffset? UpTime { get; set; }
        /// <summary>
        /// 下架时间
        /// </summary>
        [Comment("下架时间")]
        public DateTimeOffset? DownTime { get; set; }
        /// <summary>
        /// 是否参与订单折扣活动
        /// </summary>
        [Comment("是否参与订单折扣活动")]
        public bool IsDiscount { get; set; }
        /// <summary>
        /// 排序码
        /// </summary>
        [Comment("排序码")]
        public int Sort { get; set; }
        /// <summary>
        /// 是否已删除
        /// </summary>
        [Comment("是否已删除")]
        public bool IsDeleted { get; set; }
        /// <summary>
        /// 商品图片
        /// </summary>
        public virtual ICollection<ProductImageEntity> Images { get; set; }
        /// <summary>
        /// 产品分类id
        /// </summary>
        [Comment("产品分类id")]
        public int? ProductTypeId { get; set; }
        /// <summary>
        /// 产品分类对象
        /// </summary>
        public virtual ProductTypeEntity ProductType { get; set; }

        public void Configure(EntityTypeBuilder<ProductEntity> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasIndex(a => a.Name);
            entityBuilder.HasIndex(a => a.Code);
            entityBuilder.HasIndex(a => a.Pinyin);
            entityBuilder.HasIndex(a => a.FirstLetter);
            entityBuilder.HasIndex(a => a.Sort);
        }


        /// <summary>
        /// 种子
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        /// <returns></returns>
        public IEnumerable<ProductEntity> HasData(DbContext dbContext, Type dbContextLocator)
        {
            var time = new DateTimeOffset(2021, 9, 21, 8, 0, 0, default);
            return new List<ProductEntity>
            {
                new ProductEntity{ Id = 1, Name = "999感冒灵", Code = "8020210452809300523", Pinyin = "999感冒灵".ToPinyin(), FirstLetter = "999感冒灵".ToFirstLatter(), CreatedTime = time, UnitName = "盒", Price = 9.9, UpTime = time, Sort = 1, ProductTypeId = 1 },
                new ProductEntity{ Id = 2, Name = "阿莫西林", Code = "8020210452809300524", Pinyin = "阿莫西林".ToPinyin(), FirstLetter = "阿莫西林".ToFirstLatter(), CreatedTime = time, UnitName = "盒", Price = 20, UpTime = time, Sort = 2, ProductTypeId = 1 },
                new ProductEntity{ Id = 3, Name = "午时茶", Code = "8020210452809300525", Pinyin = "午时茶".ToPinyin(), FirstLetter = "午时茶".ToFirstLatter(), CreatedTime = time, UnitName = "袋", Price = 19.9, UpTime = time, Sort = 3, ProductTypeId = 1 },
                new ProductEntity{ Id = 4, Name = "皮炎平", Code = "8020210452809300526", Pinyin = "皮炎平".ToPinyin(), FirstLetter = "皮炎平".ToFirstLatter(), CreatedTime = time, UnitName = "盒", Price = 12.5, UpTime = time, Sort = 1, ProductTypeId = 4 }
            };
        }

    }
}
